在電腦科學中, 指標 是一種基本的 間接引用形式。與直接儲存值不同,指標變數儲存的是 記憶體位址——即記憶體中的特定位置——該值所存放的位置。這讓程式能協調對單一資料來源的修改,而無需付出高昂的資料複製成本。
1. 位址的邏輯
值被儲存的位置稱為其 記憶體位址。理解這一點是掌握電腦內部語言的第一步。在 Go 語言中,我們使用底線符號(&)來取得位址,並使用星號(*)來追蹤它。
2. 為何間接引用如此重要
間接引用是建立複雜、共享資料結構的強大工具。想像一個商店招牌指引訪客前往新地址。招牌本身不包含商店;它告訴你 要到哪裡去找 。Go 提供了一個安全的環境來掌握此概念:如果你之前曾接觸過指標,請深呼吸。不會那麼糟糕。如果是第一次接觸,請放輕鬆。Go 是學習指標的安全場所。
main.py
TERMINALbash — 80x24
> Ready. Click "Run" to execute.
>
QUESTION 1
Like the shop sign directing visitors to a new address, pointers direct a computer where to look for a value. What's another situation where you're directed to look somewhere else?
A URL redirecting to a new website.
Reading the final page of a book directly.
Calculating 2 + 2 in your head.
A static variable that never changes.
✅ Correct!
Correct! URLs, library index cards, and table of contents are all real-world forms of indirection.❌ Incorrect
Think about things that act as references to other locations rather than the content itself.QUESTION 2
How do you know that
time.Time never uses a pointer receiver?It is an immutable type that returns a new value for every method call.
It is a primitive type like int.
The Go compiler prohibits pointers on time structs.
It uses global variables instead.
✅ Correct!
Exactly. Methods like .Add return a new time.Time instead of modifying the existing one, which is characteristic of value receivers.❌ Incorrect
Check the documentation: methods return a new instance of Time rather than mutating the current one.QUESTION 3
What is the result of using the address operator (
&) on a variable?It returns the value stored in the variable.
It returns the memory address (location) of the variable.
It doubles the memory usage of the variable.
It deletes the variable from memory.
✅ Correct!
The ampersand is the 'address-of' operator.❌ Incorrect
The address operator finds the 'where', not the 'what'.QUESTION 4
Quick check 26.1: If a pointer is a signpost, what happens if the building it points to is painted a different color? Does the signpost need to change?
Yes, because the signpost must describe the color.
No, because the address (location) remains the same.
Yes, the pointer automatically breaks.
No, pointers only point to grayscale buildings.
✅ Correct!
Correct. Changing the data at the address doesn't change the address itself.❌ Incorrect
The pointer only cares about the location, not the internal state of the data stored there.QUESTION 5
Which statement best describes Go's approach to pointers?
It allows dangerous pointer arithmetic like C.
It is a safe environment that handles many complexities for you.
Pointers are not allowed in Go.
Pointers only work with integers.
✅ Correct!
Go is designed to be a safe place to learn and use pointers without many of the pitfalls of lower-level languages.❌ Incorrect
Review the 'Tips' section: Go is a safe place to learn pointers.Module 7 Challenge: Indirection and Sudoku Logic
Applying Pointers to Shared State Management
You are tasked with building a Sudoku validation engine. In Sudoku, the grid must be shared across various validation rules (row, column, subgrid). Passing the entire 81-element array by value would be inefficient. You must use pointer receivers to mutate the grid and handle errors gracefully.
Q
1. Write a line of code to sort a slice named 'food' from the shortest to longest string using 'sort.Slice' and a closure. [Word Count Requirement: 15 words]
Solution:
sort.Slice(food, func(i, j int) bool { return len(food[i]) < len(food[j]) })
sort.Slice(food, func(i, j int) bool { return len(food[i]) < len(food[j]) })
Q
2. If an error occurred while writing 'Clear is better than clever.' to a file in Listing 28.6, what sequence of events would follow?
Solution:
The error would be stored in the struct's error field. Subsequent write calls would check this field, find a non-nil error, and return immediately without attempting further writes, ensuring the first error is preserved.
The error would be stored in the struct's error field. Subsequent write calls would check this field, find a non-nil error, and return immediately without attempting further writes, ensuring the first error is preserved.
Q
3. Implement a method signature to clear a digit from a square in a Sudoku struct. This method need not adhere to constraints, as several squares may be empty (zero).
Solution:
func (s *Sudoku) Clear(row, col int) { s.grid[row][col] = 0 }
func (s *Sudoku) Clear(row, col int) { s.grid[row][col] = 0 }